package com.yutel.phone.binh.data.xmlhandler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.yutel.phone.binh.data.vo.Category;
import com.yutel.phone.binh.data.vo.Channel;

public class SAXforChannelHandler extends DefaultHandler {
	private ArrayList<Channel> channels;
	private Channel channel;
	private Category category;

	public ArrayList<Channel> getChannels() {
		return channels;
	}

	/**
	 * XML文档开始
	 */
	public void startDocument() throws SAXException {
		channels = new ArrayList<Channel>();
	}

	/**
	 * 开始读取内容
	 */
	public void startElement(String uri, String localname, String qname,
			Attributes attributes) throws SAXException {
		if ("channel".equals(localname)) {
			channel = new Channel();
			channel.setCategoryList(new ArrayList<Category>());
			channel.setId(attributes.getValue("id"));
			channel.setName(attributes.getValue("name"));
			channel.setIcon_url(attributes.getValue("icon"));
			channel.setFilter(attributes.getValue("filter"));
		}
		if ("category".equals(localname)) {
			category = new Category();
			category.setId(attributes.getValue("id"));
			category.setName(attributes.getValue("name"));
			category.setProgram(attributes.getValue("program"));
			category.setLogourl(attributes.getValue("logoUrl"));
		}
	}

	public void endElement(String uri, String localName, String qname)
			throws SAXException {
		if ("channel".equals(localName) && channel != null) {
			channels.add(channel);
			channel = null;
		}
		if ("category".equals(localName) && category != null) {
			channel.getCategoryList().add(category);
			category = null;
		}
	}
}
